package com.sansan.qiangji.controller.device;

import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.device.beltParam.BeltParamAddForm;
import com.sansan.qiangji.form.device.beltParam.BeltParamEditForm;
import com.sansan.qiangji.form.device.beltParam.BeltParamListForm;
import com.sansan.qiangji.realize.device.BeltParamReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@CrossOrigin()
@RestController
@RequestMapping("/beltParam")
@Slf4j
public class BeltParamController {
    BeltParamReal beltParamReal;

    @Resource
    public void setBeltParamReal(BeltParamReal beltParamReal){
        this.beltParamReal = beltParamReal;
    }

    @PostMapping("/beltParamList")
    public ResultVO<Object> beltParamList(@Valid @RequestBody BeltParamListForm beltParamListForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【传送带参数列表】参数不正确，beltParamListForm={}", beltParamListForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return ResultVOUtil.success(beltParamReal.beltParamList(beltParamListForm));
    }

    @PostMapping("/addBeltParam")
    public ResultVO<Object> addBeltParam(@Valid @RequestBody BeltParamAddForm beltParamAddForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【添加传送带参数】参数不正确，beltParamAddForm={}", beltParamAddForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        beltParamReal.addBeltParam(beltParamAddForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/editBeltParam")
    public ResultVO<Object> editBeltParam(@Valid @RequestBody BeltParamEditForm beltParamEditForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【编辑传送带参数】参数不正确，beltParamEditForm={}", beltParamEditForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        beltParamReal.editBeltParam(beltParamEditForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/deleteBeltParam")
    public ResultVO<Object> deleteBeltParam(@RequestParam(name = "beltParamId") Integer beltParamId) {
        beltParamReal.deleteBeltParam(beltParamId);
        return ResultVOUtil.success(null);
    }
}
